<title>分类管理</title>

<div class="layui-card layadmin-header">
    <div class="layui-breadcrumb" lay-filter="breadcrumb">
        <a lay-href="">主页</a>
        <a><cite>应用</cite></a>
        <a><cite>一层数据管理</cite></a>
    </div>
</div>

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-header layuiadmin-card-header-auto">
            <div
                class="layui-form"
                lay-filter="LAY-app-form-tags"
                id="LAY-app-form-tags"
            >
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">匹配模式</label>
                        <div class="layui-input-inline">
                            <select name="isExact" lay-verify="required">
                                <option value="0">精准</option>
                                <option value="1">模糊</option>
                            </select>
                        </div>
                    </div> 
                    <div class="layui-inline">
                        <label class="layui-form-label">域名</label>
                        <div class="layui-input-inline">
                            <input
                                type="text"
                                name="host"
                                autocomplete="off"
                                class="layui-input"
                            />
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">标题</label>
                        <div class="layui-input-inline">
                            <input
                                type="text"
                                name="title"
                                autocomplete="off"
                                class="layui-input"
                            />
                        </div>
                    </div>

                    <div class="layui-inline">
                        <label class="layui-form-label">IP</label>
                        <div class="layui-input-inline">
                            <input
                                type="text"
                                name="ip"
                                autocomplete="off"
                                class="layui-input"
                            />
                        </div>
                    </div>
                    
                    <div class="layui-inline">
                        <label class="layui-form-label">URL</label>
                        <div class="layui-input-inline">
                            <input
                                type="text"
                                name="url"
                                autocomplete="off"
                                class="layui-input"
                            />
                        </div>
                    </div> 
                    <br /> 

                    <div class="layui-inline">
                      <label class="layui-form-label">ID</label>
                      <div class="layui-input-inline">
                          <input
                              type="text"
                              name="ID"
                              autocomplete="off"
                              class="layui-input"
                          />
                      </div>
                  </div>

                    <div class="layui-inline">
                        <label class="layui-form-label">IP归属</label>
                        <div class="layui-input-inline">
                            <select
                                name="ip_area"
                                lay-search
                                lay-filter="ip_area"
                            >
                                <option value=""></option>
                                <option value="1">境内</option>
                                <option value="2">境外</option>
                                <option value="3">港澳台</option>
                                <option value="4">未知</option>
                            </select>
                        </div>
                    </div>

                    <div class="layui-inline">
                        <label class="layui-form-label">有无标题</label>
                        <div class="layui-input-inline">
                            <select
                                name="isTitle"
                                lay-search
                                lay-filter="isTitle"
                            >
                                <option value=""></option>
                                <option value="1">有</option>
                                <option value="2">无</option>
                            </select>
                        </div>
                    </div>
                    <br />
                    <!-- <div class="layui-inline">
                        <label class="layui-form-label">一级分类</label>
                        <div class="layui-input-inline">
                            <select name="class" lay-search lay-filter="class">
                                <option value=""></option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">二级分类</label>
                        <div class="layui-input-inline">
                            <select
                                name="class_sec"
                                lay-search
                                lay-filter="class_sec"
                            >
                                <option value=""></option>
                            </select>
                        </div>
                    </div> -->
                </div>
                <!-- <button
                    class="layui-btn layui-btn-normal layuiadmin-btn-tags"
                    data-type="choiceFields"
                    style="margin-left: 15px"
                >
                    筛选字段
                </button>
                <button
                    class="layui-btn layuiadmin-btn-tags"
                    data-type="batchfilterTitle"
                >
                    批量筛选标题
                </button>
                <button
                    class="layui-btn layuiadmin-btn-tags"
                    data-type="batchfilterUrl"
                >
                    批量筛选网址
                </button>
                <button
                    class="layui-btn layuiadmin-btn-tags"
                    data-type="uploadfile"
                >
                    导入数据
                </button>
                <a
                    class="layui-btn layuiadmin-btn-tags"
                    href="/admin/link/export"
                    id="exportFile"
                    target="_blank"
                >
                    导出数据
                </a> 

                <button
                    class="layui-btn layuiadmin-btn-tags"
                    id="showTitleCount"
                >
                    查看标题排行
                </button>
                <button class="layui-btn layuiadmin-btn-tags" id="showIpCount">
                    查看IP排行
                </button> -->
                <button
                    class="layui-btn layuiadmin-btn-tags"
                    lay-submit
                    lay-filter="LAY-app-form-tags"
                    style="margin-left: 15px"
                >
                    搜 索
                </button>
                <button
                    class="layui-btn layui-btn-danger layuiadmin-btn-tags"
                    id="dels"
                >
                    批量删除
                </button>
            </div>
            <div class="layui-card-body">
                <table
                    id="LAY-app-content-tags"
                    lay-filter="LAY-app-content-tags"
                ></table>
                <div style="height: 20px;"></div>
                <div class="loading" style="display:none;">数据加载中...</div> 

                <script type="text/html" id="layuiadmin-app-cont-tagsbar">
                    <!-- <a
                        class="layui-btn layui-btn-normal layui-btn-xs"
                        lay-event="edit"
                        ><i class="layui-icon layui-icon-edit"></i>编辑</a
                    > -->
                    <a
                        class="layui-btn layui-btn-danger layui-btn-xs"
                        lay-event="del"
                        ><i class="layui-icon layui-icon-delete"></i>删除</a
                    >
                </script>

                <script type="text/html" id="classTpl">
                    {{# if(d.class == 104){ }} 是 {{# } else { }} 否 {{# } }}
                </script>
                <script type="text/html" id="isGatherTpl">
                    {{# if(d.is_gather == 1){ }} 是 {{# } else { }} 否 {{# } }}
                </script>
            </div>
        </div>
    </div>

    <script>
        layui
            .use("contlist", layui.factory("contlist"))
            .use(["admin", "table", "upload"], function () {
                var $ = layui.$,
                    admin = layui.admin,
                    view = layui.view,
                    table = layui.table,
                    form = layui.form,
                    upload = layui.upload;

                // localStorage.setItem("title", '');
                // localStorage.setItem("ip", '');
                var host = sessionStorage.getItem("Host");
                $('input[name="host"]').val(host);

                var params = [];
                var type = "";
                if (host != undefined && host != "") {
                    params = host;
                    type = "Host";
                }

                //document.querySelector('.loading').style.display = 'block';
                var tableIns = table.render({
                    elem: "#LAY-app-content-tags",
                    url: "/index/first_floor_data",
                    method: "POST",
                    where: { params, type },
                    cols: [
                        [
                            { type: "checkbox", fixed: "left" },
                            {
                                field: "ID",
                                width: 100,
                                title: "ID",
                                sort: true,
                            },
                            {
                                field: "Host",
                                title: "一层域名",
                                minWidth: 210,
                            },
                            {
                                field: "URL",
                                title: "一层URL",
                                minWidth: 350,
                            },
                            {
                                field: "title",
                                title: "标题",
                                minWidth: 300,
                            },
                            {
                                field: "IP",
                                title: "IP",
                                minWidth: 200,
                                align: "center",
                            },
                            {
                                field: "IPArea",
                                title: "IP归属",
                                minWidth: 100,
                                align: "center",
                            },
                            {
                                field: "class",
                                title: "一级分类",
                                minWidth: 200, 
                            },
                            {
                                field: "import_source",
                                title: "数据来源",
                                minWidth: 200,
                            },
                            {
                                field: "import_time",
                                title: "导入时间",
                                minWidth: 200, 
                            },
                            {
                                field: "gather_time",
                                title: "采集时间",
                                minWidth: 200,
                                templet: function (d) {
                                    return convertTime(d.gather_time);
                                },
                            },
                            {
                                field: "marking_count",
                                title: "打标次数",
                                minWidth: 200,
                            },
                            {
                                title: "操作",
                                width: 120,
                                align: "center",
                                fixed: "right",
                                unresize: true,
                                toolbar: "#layuiadmin-app-cont-tagsbar",
                            },
                        ],
                    ],
                    page: {
                        layout: [
                            "count",
                            "prev",
                            "page",
                            "next",
                            "limit",
                            "skip",
                        ], // 分页显示在最上方
                    },
                    limit: 10,
                    limits: [10, 20, 30, 50, 100, 200],
                    text: "对不起，加载出现异常！",
                    done: function (res, curr, count) {
                        sessionStorage.setItem("Host", "");
                        if (res.data.length == 0) {
                            $(".layui-none").html("<p>无符合数据</p>");
                        }
                        //document.querySelector('.loading').style.display = 'none';
                    },
                });

                var checkBoxData = [
                    "host",
                    "url",
                    "url_host",
                    "title",
                    "ip",
                    "ip_area",
                    "jump_url",
                    "import_source",
                    "class",
                    "class_sec",
                ];
                

                function initCheckBox() {
                    $.each(checkBoxData, function (index, field) {
                        console.log(localStorage.getItem(field), field);
                        var tableId = "LAY-app-form-tags";
                        var $th = $(
                            '.layui-table-box .layui-table-header thead tr th[data-field="' +
                                field +
                                '"]'
                        );
                        var $td = $(
                            '.layui-table-box .layui-table-body .layui-table tbody tr td[data-field="' +
                                field +
                                '"]'
                        );
                        if (localStorage.getItem(field) === "true") {
                            $th.show();
                            $td.show();
                        } else {
                            $th.hide();
                            $td.hide();
                        }
                    });

                    //var currentCols = tableIns.config.cols;
                    //table.render(currentCols);
                } 


                function formatDate(timestamp) {
                  // 将字符串类型的时间转换为数字类型的时间戳 
                  var regex = /:/;
                  console.log(regex.test(timestamp));
                  if(regex.test(timestamp)){
                    var date = new Date(timestamp);
                  }else{
                    var unixTimestamp = parseInt(timestamp);
                    // 创建一个新的 Date 对象，注意要乘以 1000 转换为毫秒
                    var date = new Date(unixTimestamp * 1000);
                  }
                  
                  // 使用 Date 对象的方法获取年、月、日、时、分、秒
                  var year = date.getFullYear();
                  var month = pad(date.getMonth() + 1);
                  var day = pad(date.getDate());
                  var hours = pad(date.getHours());
                  var minutes = pad(date.getMinutes());
                  var seconds = pad(date.getSeconds());
                  // 拼接成所需的字符串格式
                  var formattedDate = year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds;
                  return formattedDate;
              } 

                function convertTime(value) {  
                  var formattedDate = formatDate(value); 
                  if(value === null){
                    return '';
                  }
                  return formattedDate; 
                }

                // 辅助函数，用于给数字添加前导零
                function pad(number) {
                    return number < 10 ? "0" + number : number;
                }
 

                form.on("submit(LAY-app-form-tags)", function (data) {
                    //document.querySelector('.loading').style.display = 'block';
                    sessionStorage.setItem("Host", "");
                    table.reload("LAY-app-content-tags", {
                        url: "/index/search",
                        method: "POST",
                        where: data.field,
                        page: { curr: 1 },
                        done: function (res, curr, count) {
                            if (res.data.length == 0) {
                                $(".layui-none").html("<p>无符合数据</p>");
                            }
                            //document.querySelector('.loading').style.display = 'none';
                        },
                    });

                    return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
                });

                //监听工具条
                table.on("tool(LAY-app-content-tags)", function (obj) {
                    var data = obj.data;
                    if (obj.event === "del") {
                        layer.confirm("确定删除此条数据？", function (index) {
                            $.ajax({
                                url: "/admin/first/delete",
                                method: "POST",
                                data: data,
                                success: function (res) {
                                    layer.msg(res.msg);
                                    layui.table.reload("LAY-app-content-tags");
                                    layer.close(index);
                                },
                            });
                        });
                    } else if (obj.event === "edit") {
                        admin.popup({
                            title: "编辑数据",
                            area: ["600px", "500px"],
                            id: "LAY-popup-content-tags",
                            success: function (layero, index) {
                                view(this.id)
                                    .render("info/info_form_edit", data)
                                    .done(function () {
                                        $('input[name="id"]').val(data.id);
                                        $('input[name="title"]').val(
                                            data.title
                                        );
                                        $('input[name="host"]').val(data.host);
                                        $('input[name="url"]').val(data.url);
                                        $('input[name="ip"]').val(data.ip);
                                        form.render();

                                        //监听提交
                                        form.on(
                                            "submit(layuiadmin-app-edit-submit)",
                                            function (data) {
                                                var field = data.field; //获取提交的字段

                                                //提交 Ajax 成功后，关闭当前弹层并重载表格
                                                admin.req({
                                                    url: "/admin/link/updateEditData",
                                                    method: "POST",
                                                    data: field,
                                                    success: function (res) {
                                                        if (res.code === 0) {
                                                            layer.msg(res.msg);
                                                            layui.table.reload(
                                                                "LAY-app-content-tags"
                                                            );
                                                            layer.close(index);
                                                        } else {
                                                            // layer.msg( res.msg );
                                                            // layui.table.reload(  "LAY-app-content-tags" );
                                                            // layer.close( index );
                                                        }
                                                    },
                                                    error: function (err) {},
                                                });

                                                layui.table.reload(
                                                    "LAY-app-content-tags"
                                                ); //重载表格
                                                layer.close(index); //执行关闭
                                            }
                                        );
                                    });
                            },
                        });
                    }
                });

                function btnOperation(title, newpage, apiName, elem, field) {
                    admin.popup({
                        title: "展示" + title + "排行",
                        area: ["600px", "600px"],
                        id: "LAY-popup-content-tags",
                        success: function (layero, index) {
                            view(this.id)
                                .render(newpage)
                                .done(function () {
                                    form.render(null, "LAY-app-form-tags");
                                    table.render({
                                        elem: elem, // 绑定已有的 table 元素
                                        url: apiName, // 数据接口地址
                                        method: "POST",
                                        cols: [
                                            [
                                                {
                                                    field: field,
                                                    title: title,
                                                },
                                                {
                                                    field: "count",
                                                    title: "数量",
                                                },
                                            ],
                                        ],
                                    });
                                });
                        },
                    });
                }

                layui.$("button#showIpCount").on("click", function () {
                    btnOperation(
                        "IP",
                        "info/info_show_ip",
                        "/admin/first/showIp",
                        "#showIpData-tags",
                        "ip"
                    );
                });
                layui.$("button#showTitleCount").on("click", function () {
                    btnOperation(
                        "标题",
                        "info/info_show_title",
                        "/admin/first/showTitle",
                        "#showTilteData-tags",
                        "title"
                    );
                });
                layui.$("button#dels").on("click", function () {
                    clickForAjax("/admin/first/delete");
                });

                function clickForAjax(url) {
                    var checkStatus = table.checkStatus("LAY-app-content-tags");
                    var data = checkStatus.data;
                    var ids = [];
                    for (i = 0; i < data.length; i++) {
                        ids.push(data[i].ID);
                    }
                    admin.req({
                        url: url,
                        method: "POST",
                        data: { ids },
                        success: function (res) {
                            if (res.code === 0) {
                                layer.msg(res.msg);
                                layui.table.reload("LAY-app-content-tags"); //重载表格
                                layer.close(); //执行关闭
                            } else {
                                layer.msg(res.msg);
                                layui.table.reload("LAY-app-content-tags");
                            }
                        },
                    });
                }

                var active = {
                    batchfilterTitle: function () {
                        admin.popup({
                            title: "批量筛选标题",
                            area: ["800px", "600px"],
                            id: "LAY-popup-content-tags",
                            success: function (layero, index) {
                                view(this.id)
                                    .render("info/info_form")
                                    .done(function () {
                                        form.render(null, "LAY-app-form-tags");
                                        //监听提交
                                        form.on(
                                            "submit(layuiadmin-app-tags-submit)",
                                            function (data) {
                                                var field = data.field; //获取提交的字段
                                                $('input[name="url"]').val("");
                                                $('input[name="ip"]').val("");
                                                table.reload(
                                                    "LAY-app-content-tags",
                                                    {
                                                        url: "/admin/link/batchSearchTitle",
                                                        where: data.field,
                                                        page: { curr: 1 },
                                                        done: function (
                                                            res,
                                                            curr,
                                                            count
                                                        ) {
                                                            if (
                                                                res.data
                                                                    .length == 0
                                                            ) {
                                                                $(
                                                                    ".layui-none"
                                                                ).html(
                                                                    "<p>无符合数据</p>"
                                                                );
                                                            }
                                                            layer.close(index);
                                                        },
                                                    }
                                                );
                                            }
                                        );
                                    });
                            },
                        });
                    },
                    batchfilterUrl: function () {
                        admin.popup({
                            title: "批量筛选网址",
                            area: ["800px", "600px"],
                            id: "LAY-popup-content-tags",
                            success: function (layero, index) {
                                view(this.id)
                                    .render("info/info_form")
                                    .done(function () {
                                        form.render(null, "LAY-app-form-tags");
                                        $("#exactBtn").addClass("hidden");

                                        //监听提交
                                        form.on(
                                            "submit(layuiadmin-app-tags-submit)",
                                            function (data) {
                                                var field = data.field; //获取提交的字段
                                                table.reload(
                                                    "LAY-app-content-tags",
                                                    {
                                                        url: "/admin/link/batchSearchUrl",
                                                        where: data.field,
                                                        page: { curr: 1 },
                                                        done: function (
                                                            res,
                                                            curr,
                                                            count
                                                        ) {
                                                            if (
                                                                res.data
                                                                    .length == 0
                                                            ) {
                                                                $(
                                                                    ".layui-none"
                                                                ).html(
                                                                    "<p>无符合数据</p>"
                                                                );
                                                            }
                                                            $(
                                                                ".showNoData"
                                                            ).empty();
                                                            if (
                                                                res.noData
                                                                    .length > 0
                                                            ) {
                                                                $(
                                                                    ".showNoData"
                                                                ).append(
                                                                    "<li>不在库的网址有：</li>"
                                                                );
                                                                res.noData.forEach(
                                                                    function (
                                                                        item
                                                                    ) {
                                                                        $(
                                                                            ".showNoData"
                                                                        ).append(
                                                                            "<li>" +
                                                                                item +
                                                                                "</li>"
                                                                        );
                                                                    }
                                                                );
                                                                $(
                                                                    ".showNoData"
                                                                ).append(
                                                                    "<li>不在库的网址有?条</li>"
                                                                );
                                                                $(
                                                                    ".showNoData"
                                                                ).append(
                                                                    "<li>" +
                                                                        res.noDataCount +
                                                                        "</li>"
                                                                );
                                                            } else {
                                                                layer.close(
                                                                    index
                                                                );
                                                            }
                                                        },
                                                    }
                                                );
                                            }
                                        );
                                    });
                            },
                        });
                    },
                    choiceFields: function () {
                        admin.popup({
                            title: "筛选table字段",
                            area: ["600px", "400px"],
                            id: "LAY-popup-content-tags",
                            success: function (layero, index) {
                                view(this.id)
                                    .render("info/info_show_field")
                                    .done(function () {
                                        form.render(null, "LAY-app-form-tags");
                                        //监听提交
                                        layui.form.on(
                                            "checkbox(colCheckbox)",
                                            function (obj) {
                                                var field =
                                                    obj.elem.getAttribute(
                                                        "data-field"
                                                    );
                                                var checked = obj.elem.checked;
                                                localStorage.setItem(
                                                    field,
                                                    checked
                                                );

                                                var tableId =
                                                    "LAY-app-form-tags";
                                                var $th = $(
                                                    '.layui-table-box .layui-table-header thead tr th[data-field="' +
                                                        field +
                                                        '"]'
                                                );
                                                var $td = $(
                                                    '.layui-table-box .layui-table-body .layui-table tbody tr td[data-field="' +
                                                        field +
                                                        '"]'
                                                );
                                                if (checked) {
                                                    $th.show();
                                                    $td.show();
                                                } else {
                                                    $th.hide();
                                                    $td.hide();
                                                }
                                            }
                                        );
                                    });
                            },
                        });
                    },
                    uploadfile: function () {
                        admin.popup({
                            title: "导入网址",
                            area: ["800px", "600px"],
                            id: "LAY-popup-content-tags",
                            success: function (layero, index) {
                                view(this.id)
                                    .render("info/upload_file")
                                    .done(function () {
                                        form.render(null, "LAY-app-form-tags");
                                        //监听提交
                                        form.on(
                                            "submit(layuiadmin-app-upload-submit)",
                                            function (data) {
                                                var url = data.field.addUrl; //获取提交的字段
                                                admin.req({
                                                    url: "/admin/link/addUrl",
                                                    method: "POST",
                                                    data: data.field,
                                                    success: function (res) {
                                                        layer.msg(
                                                            res.msg,
                                                            { time: 500 },
                                                            function () {
                                                                setTimeout(
                                                                    function () {
                                                                        window.location.reload();
                                                                    },
                                                                    1
                                                                );
                                                            }
                                                        );
                                                        //layer.close(index);
                                                    },
                                                    error: function (err) {
                                                        console.error(err);
                                                    },
                                                });
                                            }
                                        );
                                    });
                            },
                        });
                    },
                };
                $(".layui-btn.layuiadmin-btn-tags").on("click", function () {
                    var type = $(this).data("type");
                    active[type] ? active[type].call(this) : "";
                });
            });
    </script>

    <script
        src="/admin/start/layui/formSelects-v4.js"
        type="text/javascript"
        charset="utf-8"
    ></script>
    <link rel="stylesheet" href="/admin/start/layui/formSelects-v4.css" />
    <style rel="stylesheet">
        #LAY_app_body .layui-table-loading {
          display: none !important; /* 隐藏加载图标 */
        }
        #LAY_app_body .layadmin-loading {
           position: absolute; 
           left: -10%; 
           top: -50%; 
        }
        .layui-table th {
            text-align: center;
        }
        .layui-form-label {
            float: left;
            display: block;
            padding: 9px 6px;
            width: 80px;
            font-weight: 400;
            line-height: 20px;
            text-align: right;
        }
        .loading {
          text-align: center;
          padding: 10px;
          background-color: rgba(30, 231, 23, 0.5);
          color: white;
          position: absolute;
          border-radius: 5px;
          font-size: 16px;
          top: 60%;
          left: 50%;
          transform: translate(-50%, -50%);
        }
    </style>
</div>
